from BaseController import BaseController
#import csv
#import StringIO
#from sets import Set
from Utility import SessionKey
#from Common.HIT import HIT
#from Common.Campaign import Campaign
#import re
from Biz.CsvHandler import HITCsvHandler

class UploadCsvController(BaseController):
    def get(self):
        self.Render("test_upload", None)
    
    def post(self):
        
        #
        # Here should get data from form.
        #
        
#        """
#        Gets campaign data from session
#        """
#        campaign = self.GetSessionItem(SessionKey.CURRENT_HIT_CAMPAIGN)
#        if  campaign is None:
#            return self.response.out.write("Campaign could not be null")
#        
#        """
#        Gets QuestionFormTemplate data from session
#        """
#        formTemplate = self.GetSessionItem(SessionKey.CURRENT_HIT_FormTemplateData)
#        if formTemplate is None:
#            return self.response.out.write("Question template could not be null")

        #
        # Get template Html segment by template name
        #
        
#       campaign = None
        
        #
        # it should get from db
        #
        
        formTemplate = """
            Write a ${wordcount} word article on the following topic: Types of Music and Musicians from <h1>${h1_tag}</h1>
            <br />
            This article is about ${category}
            <br />
            <ul>
            <li>Article should be ${wordcount} words or more</li>
            <li>Must create headers to separate each paragraph</li>
            <li>Use the keyword phrase " ${primary_keyword} " ${keywordusage_primary} times in the article. Only use it ${keywordusage_primary} times.</li>
            <li>Use the keyword phrase " ${secondary_keyword} " ${keywordusage_secondary} times in the article. Only use it ${keywordusage_secondary} times.</li>
            <li>... ...</li>
            </ul>
            """       
        
        handleResult= HITCsvHandler(self.request.get("uploadfile"),formTemplate)
        if not handleResult["isok"]:
            self.response.out.write(handleResult["message"])
        
        self.SetSessionItem(SessionKey.CURRENT_HIT_DATA, handleResult["hitdata"])
        self.SetSessionItem(SessionKey.CURRENT_HIT_CSV_DATA, handleResult["csvdata"])
        return self.redirect("/preview/hit")

